﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Collections.Generic;
using KPIS.GERP.GIMS.MODEL;
using KPIS.GERP.GIMS.DAL;
using Telerik.Web.UI;
using System.Web.UI.WebControls;

namespace KPIS.GERP.GIMS.APP.APP_UI.Add_Form
{
    public partial class ICM_Inventory_Stock_Taking : System.Web.UI.Page
    {
        Common.Main cn = new Common.Main();
        D_JobSiteSeq d_job_site = new D_JobSiteSeq();
        M_JobSiteSeq m_job_site = new M_JobSiteSeq();
        D_Warehouse d_warehouse = new D_Warehouse();
        MasterWarehouseInfo m_warehouse = new MasterWarehouseInfo();
        D_IcmInventoryStockTaking d_stock_taking = new D_IcmInventoryStockTaking();
        M_IcmInventoryStockTaking m_stock_taking = new M_IcmInventoryStockTaking();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                EnableFalseRadio();
                pCheckStockCode.Text = fncGenerate_StockTakingCode();
            }
        }
        protected void btnSave_Click(object sender, EventArgs e)
        {
            if (chkValidate())
            {
                try
                {
                    m_stock_taking.icm_stock_taking_code = pCheckStockCode.Text;
                    m_stock_taking.stock_taking_start_date = Convert.ToDateTime(txtCheckStartDate.TextWithLiterals);
                    if (txtCheckEndDate.Text !="")
                    m_stock_taking.stock_taking_end_date = cn.ConvertNullDateTime(txtCheckEndDate.TextWithLiterals);
                    m_stock_taking.stock_taking_desc = txtCheckStockDesc.Text;
                    m_stock_taking.stock_taking_remark = txtRemark.Text;
                    m_stock_taking.job_site_seq = cn.ConvertNullInt(cmbJobSite.SelectedValue);
                    m_stock_taking.icm_wh_seq = cn.ConvertNullInt(cmbWarehouse.SelectedValue);
                    m_stock_taking.record_status = "U";
                    m_stock_taking.created_by = cn.UserID;
                    m_stock_taking.created_when = DateTime.Now;
                    m_stock_taking.approved_by = cn.ApproveID;
                    d_stock_taking.Insert_Stock_Taking(m_stock_taking);
                    EnableObject(false);
                    RadAjaxManager1.Alert("บันทึกข้อมูลเรียบร้อยแล้ว");
                }
                catch (Exception ex)
                {
                    RadAjaxManager1.Alert("เกิดข้อผิดพลาดระหว่างบันทึกข้อมูล");
                    throw (ex);
                }
                finally
                {
                    RadAjaxManager1.ResponseScripts.Add(@"CloseWindow();");
                }
            }
        }
        protected void btnCancel_Click(object sender, EventArgs e)
        {
            ClearObject();
        }
        protected void rbJobSite_CheckedChanged(object sender, EventArgs e)
        {
            ChooseJoBSite(true);
            cmbWarehouse.SelectedValue = "";
            cmbWarehouse.Text = "";
        }
        protected void rbWarehouse_CheckedChanged(object sender, EventArgs e)
        {
            ChooseJoBSite(false);
            cmbJobSite.SelectedValue = "";
            cmbJobSite.Text = "";
        }
        //============================ Set Object  ===========================
        protected void cmbJobSite_ItemsRequested(object o, RadComboBoxItemsRequestedEventArgs e)
        {
            IList<M_JobSiteSeq> infoList = d_job_site.GetListFilterByName(e.Text);
            int itemOffset = e.NumberOfItems;
            int endOffset = Math.Min(itemOffset + 10, infoList.Count);
            e.EndOfItems = (endOffset == infoList.Count);
            int j = 0;
            for (int i = itemOffset; i < endOffset; i++)
            {
                cmbJobSite.Items.Add(new RadComboBoxItem(infoList[i].job_site_name, infoList[i].job_site_seq.ToString()));
                TableRow tr = new TableRow();
                TableCell tdCode = new TableCell();
                Label lblCode = new Label();
                lblCode.Text = infoList[i].job_site_ref;
                tdCode.Width = Unit.Pixel(200);
                tdCode.Controls.Add(lblCode);
                tr.Cells.Add(tdCode);
                TableCell tdName = new TableCell();
                Label lblName = new Label();
                lblName.Text = infoList[i].job_site_name;
                tdName.Width = Unit.Pixel(200);
                tdName.Controls.Add(lblName);
                tr.Cells.Add(tdName);
                ((Table)cmbJobSite.Items[j].FindControl("tableJoBSite")).Controls.Add(tr);
                j++;
            }
            e.Message = GetStatusMessage(endOffset, infoList.Count);
        }
        protected void cmbWarehouse_ItemsRequested(object o, RadComboBoxItemsRequestedEventArgs e)
        {
            IList<MasterWarehouseInfo> infoList = d_warehouse.GetListFillByName(e.Text);
            int itemOffset = e.NumberOfItems;
            int endOffset = Math.Min(itemOffset + 10, infoList.Count);
            e.EndOfItems = (endOffset == infoList.Count);
            int j = 0;
            for (int i = itemOffset; i < endOffset; i++)
            {
                cmbWarehouse.Items.Add(new RadComboBoxItem(infoList[i].Name, infoList[i].Seq.ToString()));
                TableRow tr = new TableRow();
                TableCell tdName = new TableCell();
                Label lblName = new Label();
                lblName.Text = infoList[i].Name;
                tdName.Width = Unit.Pixel(200);
                tdName.Controls.Add(lblName);
                tr.Cells.Add(tdName);
                TableCell tdDesc = new TableCell();
                Label lblDesc = new Label();
                lblDesc.Text = infoList[i].Descr;
                tdDesc.Width = Unit.Pixel(200);
                tdDesc.Controls.Add(lblDesc);
                tr.Cells.Add(tdDesc);
                ((Table)cmbWarehouse.Items[j].FindControl("tableWarehouse")).Controls.Add(tr);
                j++;
            }
            e.Message = GetStatusMessage(endOffset, infoList.Count);
        }
        private static string GetStatusMessage(int offset, int total)
        {
            if (total <= 0)
                return "ไม่มีข้อมูล";

            return String.Format("รายการที่ <b>1</b>-<b>{0}</b> จากทั้งหมด <b>{1}</b> รายการ", offset, total);
        }
        //======================= Function ==========================//
        private void EnableFalseRadio()
        {
            lblJoBSiteSendRepair.Visible = false;
            lblWarehouse.Visible = false;
            cmbJobSite.Visible = false;
            cmbWarehouse.Visible = false;
            lblWarningOrg.Visible = false;
        }
        private void ChooseJoBSite(bool pFlag)
        {
            lblJoBSiteSendRepair.Visible = pFlag;
            cmbJobSite.Visible = pFlag;
            lblWarehouse.Visible = !pFlag;
            cmbWarehouse.Visible = !pFlag;
            lblWarningOrg.Visible = true;
        }
        private void ClearObject()
        {
            txtCheckStartDate.Text = "";
            txtCheckEndDate.Text = "";
            txtCheckStockDesc.Text = "";
            txtRemark.Text = "";
            cmbJobSite.Text = "";
            cmbJobSite.ClearSelection();
            cmbWarehouse.Text = "";
            cmbWarehouse.ClearSelection();
            rbJobSite.Checked = false;
            rbWarehouse.Checked = false;
        }
        private void EnableObject(bool pFlag)
        {
            txtCheckStartDate.Enabled = pFlag;
            txtCheckEndDate.Enabled = pFlag;
            txtCheckStockDesc.Enabled = pFlag;
            txtRemark.Enabled = pFlag;
            cmbJobSite.Enabled = pFlag;
            cmbWarehouse.Enabled = pFlag;
            rbJobSite.Enabled = pFlag;
            rbWarehouse.Enabled = pFlag;
            btnSave.Enabled = pFlag;
            btnCancel.Enabled = pFlag;
        }
        private bool chkValidate()
        {
            bool pFlag = true;
            if (txtCheckStartDate.Text == "")
            {
                pFlag = false;
                txtCheckStartDate.Focus();
                RadAjaxManager1.Alert("ตรวจสอบวันที่รับเข้า");
            }
            else if (txtCheckStockDesc.Text == "")
            {
                pFlag = false;
                txtCheckStockDesc.Focus();
                RadAjaxManager1.Alert("กรอกรายการตรวจนับด้วย");
            }
            else if ((cmbJobSite.SelectedValue == "") && (cmbWarehouse.SelectedValue == ""))
            {
                pFlag = false;
                RadAjaxManager1.Alert("กรุณากรอกสถานที่ตรวจนับด้วย");
            }
            return pFlag;
        }
        private string fncGenerate_StockTakingCode()
        {
            string strMonth = "";
            string strYear = "";
            string strRunning = "";
            string strStockTaking = "";
            try
            {
                M_IcmInventoryStockTaking info = d_stock_taking.GenerateStockTakingCode();
                strYear = info.str_year;
                strMonth = cn.ConvertstrMonth(info.str_month);
                strRunning = cn.ConvertstrRunningFiveDigit(info.stock_taking_running);
                strStockTaking = "ST" + strYear + "-" + strMonth + "-" + strRunning;
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return strStockTaking;
        }
    }
}
